// pages/user/luckDraw/index.js
const appGlobalData = getApp().globalData;
const http = require('../../../utils/http.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag:false
  },
  jpBtn:function(){
    wx.navigateTo({
      url: '/pages/user/mycoupon/index'
    })
  },
  jfBtn:function(){//积分抽奖
    if (!appGlobalData.userInfo.mobile) {//授权了，可以获取用户信息了
      //重定向到指定的页面
      wx.showModal({
        content: '为了给您提供更好的服务，请登录后使用',
        success (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            wx.navigateTo({
              url: "/pages/auth/auth"
            });
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    }else{
      var that=this
      wx.showModal({
        title: '提示',
        content: '你是否确定使用'+that.data.jfNum+'积分抽奖？',
        success (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            var arr=Array()
            arr.subMchId=that.data.subMchId
            arr.gameId=that.data.gameId
            http.get('/api/purchase/pointsLottery', arr).then(res2 => {
              wx.hideLoading()
              if (res2.code == 200) {
                  that.setData({
                    jpInfo:res2.data,
                    txImg:wx.getStorageSync('userInfo').avatarUrl
                  })
                  setTimeout(function(){
                    that.setData({
                      flag:true
                    })
                    //console.log(res2.data.millisecond)
                  },res2.data.index*res2.data.millisecond+1000)
              } else {
                  wx.showModal({
                      content: res2.message,
                      showCancel: false,
                  })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  gmbtn:function(){
    if (!appGlobalData.userInfo.mobile) {//授权了，可以获取用户信息了
      //重定向到指定的页面
      wx.showModal({
        content: '为了给您提供更好的服务，请登录后使用',
        success (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
            wx.navigateTo({
              url: "/pages/auth/auth"
            });
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    }else{
      var that=this
      wx.showModal({
        title: '提示',
        content: '你是否确定购买？',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            var arr=Array()
            arr.subMchId=that.data.subMchId
            arr.gameId=that.data.gameId
            http.get('/api/purchase/lotteryPay', arr).then(res => {
              //console.log(res.data.outTradeNo)
              // that.setData({
              //   outTradeNo:res.data.outTradeNo
              // })
              //console.log(res.data.totalAmount)
              wx.hideLoading()
              if(res.data.totalAmount==0){//不收钱  直接抽奖
                let outTradeNo = res.data.outTradeNo
                http.get('/api/purchase/beginLottery', {outTradeNo:outTradeNo}).then(res2 => {
                  
                  if (res2.code == 200) {
                      that.setData({
                        jpInfo:res2.data,
                        txImg:wx.getStorageSync('userInfo').avatarUrl
                      })
                      setTimeout(function(){
                        that.setData({
                          flag:true
                        })
                        //console.log(res2.data.millisecond)
                      },res2.data.index*res2.data.millisecond+1000)
                  } else {
                      wx.showModal({
                          content: res2.message,
                          showCancel: false,
                      })
                  }
                })
                return false;
              }
              
              if (res.code == 200) {
                  //需要购买发起支付
                    let outTradeNo = res.data.outTradeNo
                    wx.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: res.data.signType,
                        paySign: res.data.paySign,
                        success(res1) {
                          if (res1.errMsg = "requestPayment:ok") {
                            http.get('/api/purchase/beginLottery', {outTradeNo:outTradeNo}).then(res2 => {
                              wx.hideLoading()
                              
                              if (res2.code == 200) {
                                  that.setData({
                                    jpInfo:res2.data,
                                    txImg:wx.getStorageSync('userInfo').avatarUrl
                                  })
                                  setTimeout(function(){
                                    that.setData({
                                      flag:true
                                    })
                                    //console.log(res2.data.millisecond)
                                  },res2.data.index*res2.data.millisecond+1000)
                              } else {
                                  wx.showModal({
                                      content: res2.message,
                                      showCancel: false,
                                  })
                              }
                            })
                          }
                        }
                    })
              } else {
                  wx.showModal({
                      content: res.message,
                      showCancel: false,
                  })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  jpInfoBtn:function(){
    this.setData({
      flag:false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var scene
    if(options.scene){
      scene = decodeURIComponent(options.scene)
      scene=scene.split('&')
      this.setData({
        subMchId:scene[0],
        gameId:scene[1]
      })
    }
    http.get('/api/purchase/initLotteryGame').then(res => {
      if(res.code==200){
        this.setData({
          jfNum:res.data.integral
        })
      }
    })
    // console.log(options)
    // console.log(this.data.scene)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})